<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);

class guest extends admin
{

    private $db;

    public $site;
    public $award=array();


    function __construct()
    {
        parent::__construct();
        pc_base::load_app_func('global');
        $this->db = pc_base::load_model('contest_talent_model'); // 加载model
        $this->siteid = get_siteid(); // 获取当前站点
        $this->admin_username = param::get_cookie('admin_username'); // 获取当前登录的管理员
        $model_setting=pc_base::load_model('site_setting_model');
        $this->talent_source= $model_setting->getValue("talent_source",$this->siteid);//获取人才来源
        $this->talent_xl = $model_setting->getValue("talent_xl",$this->siteid);//获取人才学历
        $city = $model_setting->getValue("city",$this->siteid);//获取城市
        $new_city = array();
        foreach ($city as $k=>$v){
        	$new_city[$v] = $v;
        }
        $this->city = $new_city;
        $province = $model_setting->getValue("talent_province",$this->siteid);//获取人才省份
        $talent_province = array();
        foreach ($province as $v){
        	$talent_province[$v] = $v;
        }
        $this->province = $talent_province;


    }

//列表
    public function init()
    {

        if (! isset($_REQUEST['contest_id']) || empty($_REQUEST['contest_id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $show_dialog = $show_validator = true;
        $show_header = '';
        $contest_id= $_REQUEST['contest_id'];
        $id= $_REQUEST['contest_id'];
        $model_contest = pc_base::load_model('contest_model'); // 加载model
        $data = $model_contest->get_one(array(
            'id' => $contest_id
        ));

        $sql="select t.*,c.id as cid,c.type as ctype from nsrweb_contest_talent as c LEFT JOIN nsrweb_talent as t on c.talent_id =t.id where t.status=1 and c.type in (2,3)   ";
        $sql .= " and  c.contest_id=".$id." and siteid =" . $this->siteid;
        $sql .= " order by c.update_time ";
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        $contestinfo = $this->db->my_listinfo($sql, $page, 10);
        $admin_model = pc_base::load_model('admin_model'); // 加载admin_model
        $administrators = $admin_model->getRealnameInfo();
        foreach ($contestinfo as $key=>$value){
            foreach ($administrators as $key2=>$value2){
                if($value['updated_by'] == $key2){
                    $value['publisher'] = $value2;
                    $contestinfo[$key] = $value;
                    break;
                }
            }
        }

        $pages = $this->db->pages;

        include $this->admin_tpl('contest_guest_list');
    }

    /**
     * 添加
     */
    public function add()
    {

        if (isset($_POST['dosubmit'])) {
            //添加模特
            $model_moter=pc_base::load_model('talent_model');//加载model
            $info=$model_moter->check_judges($_POST['talent']);
            $result = $model_moter->insert($info,true);
            if ($result) {
                $data['created_by'] =  $_SESSION['userid'];
                $data['updated_by'] =  $_SESSION['userid'];

                $data['create_time'] = date("Y-m-d H:i:s",time());
                $data['update_time'] = date("Y-m-d H:i:s",time());
                $data['contest_id'] = $_POST['contest_id'];
                $data['talent_id'] = $result;
                $data['type']=$_POST['contest_type'];


                $res = $this->db->insert($data);
                if ($res) {
                    showmessage(L('add_success'), '?m=contest&c=guest&a=init&contest_id='.$data['contest_id'],'','add');
                } else {
                    showmessage(L('add_error'), HTTP_REFERER);
                }
            } else {
                showmessage(L('add_moter_error'),HTTP_REFERER);
            }

        } else {
            $show_dialog = $show_validator = true;
            $show_header = '';
            if (! isset($_REQUEST['contest_id']) || empty($_REQUEST['contest_id'])) {
                showmessage(L('illegal_action'), HTTP_REFERER);
            }
            $contest_id= $_REQUEST['contest_id'];
            pc_base::load_sys_class('format', '', 0);
            pc_base::load_sys_class('form', '', 0);
            $action_source="contestant";
            $contest_type=2;
            include $this->admin_tpl('judges_add',"judges");
        }
    }

    /**
     * 添加
     */
    public function addGuest()
    {

        if (isset($_POST['dosubmit'])) {
            //添加模特
            $model_moter=pc_base::load_model('talent_model');//加载model
            $info=$model_moter->check_guest($_POST['talent']);
            $result =$model_moter->insert($info,true);
            if ($result) {
                $data['created_by'] =  $_SESSION['userid'];
                $data['updated_by'] =  $_SESSION['userid'];

                $data['create_time'] = date("Y-m-d H:i:s",time());
                $data['update_time'] = date("Y-m-d H:i:s",time());
                $data['contest_id'] = $_POST['contest_id'];
                $data['talent_id'] = $result;
                $data['type']=$_POST['contest_type'];

                $res = $this->db->insert($data);
                if ($res) {
                    showmessage(L('add_success'), '?m=contest&c=guest&a=init&contest_id='.$data['contest_id'],'','addGuest');
                } else {
                    showmessage(L('add_error'), HTTP_REFERER);
                }
            } else {
                showmessage(L('add_moter_error'),HTTP_REFERER);
            }

        } else {
            $show_dialog = $show_validator = true;
            $show_header = '';
            if (! isset($_REQUEST['contest_id']) || empty($_REQUEST['contest_id'])) {
                showmessage(L('illegal_action'), HTTP_REFERER);
            }
            $contest_id= $_REQUEST['contest_id'];
            pc_base::load_sys_class('format', '', 0);
            pc_base::load_sys_class('form', '', 0);
            $action_source="contestant";
            $contest_type=3;
            include $this->admin_tpl('guest_add',"guest");
        }
    }


    /**
     * 编辑
     */
    public function edit()
    {
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
        $data = $this->db->get_one(array(
            'id' => $id
        ));
        $model_award= pc_base::load_model('contest_award_model'); // 加载model
        $this->award= $model_award->getList($data['contest_id']);

        $upinfo = $_POST['data'];
        if (isset($_POST['dosubmit'])) {

            $data = isset($_POST['data']) ? $_POST['data'] : showmessage(L('illegal_parameters'), HTTP_REFERER);

            $data['updated_by'] =  $_SESSION['userid'];

            $data['update_time'] = date("Y-m-d H:i:s",time());
            $data['siteid'] = $this->siteid;
            $data['status'] = 1;
            $result = $this->db->update($data, array(
                'id' => $id
            ));
            if ($result) {
                showmessage(L('edit_success'), '?m=contest&c=winner&a=init&id='.$data['contest_id'],'','edit');
            } else {
                showmessage(L('edit_error'), HTTP_REFERER);
            }
        } else {
            //$show_dialog = $show_validator = true;
            $show_dialog = $show_validator = true;
            $show_header = '';

            include $this->admin_tpl('contest_winner_edit');
        }
    }

    /**
     * 删除、批量删除
     */
    public function del()
    {
        if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = $_REQUEST['id'];

        if (is_array($id)) {
            foreach ($id as $k => $v) {
                if (intval($v)) {
                    $id[$k] = intval($v);
                } else {
                    unset($id[$k]);
                }
            }
            $id = implode('\',\'', $id);
        }
        $result = $this->db->delete("id in ('$id')");


        if ($result) {
            showmessage(L('del_success'), HTTP_REFERER);
        } else {
            showmessage(L('del_error'), HTTP_REFERER);
        }
    }

    public function bindTalent(){
        $contest_id= $_REQUEST['contest_id'];
        $model_moter=pc_base::load_model('talent_model');//加载model
        $where=" contest_id=".$contest_id." and  type in (2,3)";
        $talent_array=$this->db->select($where,"talent_id");

        $where=" (is_judge=1 or is_guest=1) and  status=1 and  siteid =" . $this->siteid;
        // 搜索框
        $keyword = isset($_GET['keywords']) ? $_GET['keywords'] : '';
        if (isset($_GET['search'])) {
            if ($keyword) {
                $where .= " and `real_name` LIKE '%$keyword%'";
            }
        }

        if(!empty($talent_array)){
            foreach ($talent_array as $k => $v) {
                    $id[] = intval($v['talent_id']);
            }
            $id = implode('\',\'', $id);
            $where.=" and id not in ('$id')";
        }
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        $infos=$model_moter->listinfo($where,'id DESC',$page,10);
        $admin_model = pc_base::load_model('admin_model'); // 加载admin_model
        $administrators = $admin_model->getRealnameInfo();
        foreach ($infos as $key=>$value){
            foreach ($administrators as $key2=>$value2){
                if($value['updated_by'] == $key2){
                    $value['publisher'] = $value2;
                    $infos[$key] = $value;
                    break;
                }
            }
        }
        $pages = $model_moter->pages;
        $show_dialog = $show_validator = true;
        $show_header = '';
        include $this->admin_tpl('contest_guest_select');



    }

    public function bindTalentAjax(){
        $contest_id= $_REQUEST['contest_id'];
        $relation=$_REQUEST['relation'];
        $talent=explode("|",$relation);
        $data=array();
        foreach($talent as $k=>$v){
            $data[$k]['contest_id']=$contest_id;
            $data[$k]['talent_id']=$v;

            $model_moter=pc_base::load_model('talent_model');//加载model
            $where=" id=".$v;
            $talent_info=$model_moter->get_one($where);
            if($talent_info['is_judge']==1){
                $type=2;
            }elseif($talent_info['is_guest']==1){
                $type=3;
            }
            $data[$k]['type']=$type;
            $data[$k]['created_by'] =  $_SESSION['userid'];
            $data[$k]['updated_by'] =  $_SESSION['userid'];

            $data[$k]['create_time'] = date("Y-m-d H:i:s",time());
            $data[$k]['update_time'] = date("Y-m-d H:i:s",time());
        }
        $res=$this->db->multiInsert($data);
        if($res){
            echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"选择成功"));
        }else{
            echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"选择失败"));
        }
    }




}